package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.JSONResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    // 处理查询所有部门请求
    @RequiresPermissions(value = {"department:list", "部门列表"}, logical = Logical.OR)
    @RequestMapping("/list")
    //@RequiredPermission(name = "部门列表")
    public String list(Model model, QueryObject qo) {
        Subject currentUser = SecurityUtils.getSubject();
        //System.out.println("admin: " + currentUser.hasRole("admin"));
        //System.out.println("employee:delete: " + currentUser.isPermitted("employee:delete"));
        //PageResult<Department> pageResult = departmentService.query(qo);
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "department/list";  // 找视图 /WEB-INF/views/department/list.ftl
    }

    // 处理删除所有部门请求
    @RequiresPermissions(value = {"department:delete", "部门删除"}, logical = Logical.OR)
    @RequestMapping("/delete")
    //@RequiredPermission(name = "部门删除")
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            if (id != null) {
                // 传id值就删除对应的部门
                departmentService.delete(id);
            }
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "操作失败, 请联系管理员");
        }

    }

    // 处理去新增的请求 http://localhost/department/input.do
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        // 传id 去修改
        if (id != null) {
            model.addAttribute("department", departmentService.get(id));
            return "department/input";
        }
        // 无id 去新增
        return "department/input";  // 找视图 /WEB-INF/views/department/input.jsp
    }

    // 处理新增或修改请求 http://localhost/department/saveOrUpdate.do
    @RequestMapping("/saveOrUpdate")
    //@RequiredPermission(name = "部门新增/修改")
    @RequiresPermissions(value = {"department:saveOrUpdate", "部门新增/修改"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(Department department) {
        if (department.getId() != null) {
            departmentService.update(department);
        } else {
            departmentService.save(department);
        }
        return new JSONResult();
    }
}


